<template>
  <div class="refund-receipt-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>退款收料</span>
          <el-button type="primary" @click="handleCreate"
            >新建退款收料</el-button
          >
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="退料单号">
          <el-input v-model="queryForm.refundNo" placeholder="请输入退料单号" />
        </el-form-item>
        <el-form-item label="供应商">
          <el-select
            v-model="queryForm.supplier"
            filterable
            remote
            placeholder="请选择供应商"
          >
            <el-option
              v-for="item in supplierOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="退料日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="请选择状态">
            <el-option label="待收料" value="pending" />
            <el-option label="已完成" value="completed" />
            <el-option label="已取消" value="cancelled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="expand">
          <template #default="props">
            <el-table :data="props.row.items" border>
              <el-table-column prop="materialCode" label="物料编码" />
              <el-table-column prop="materialName" label="物料名称" />
              <el-table-column prop="refundQuantity" label="退料数量" />
              <el-table-column prop="receivedQuantity" label="已收数量" />
              <el-table-column prop="remainQuantity" label="待收数量" />
              <el-table-column
                prop="reason"
                label="退料原因"
                show-overflow-tooltip
              />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="refundNo" label="退料单号" width="180" />
        <el-table-column prop="supplierName" label="供应商" />
        <el-table-column prop="refundDate" label="退料日期" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button
              link
              type="primary"
              @click="handleReceive(scope.row)"
              v-if="scope.row.status === 'pending'"
              >收料</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 退料收料对话框 -->
    <el-dialog v-model="receiveDialogVisible" title="退料收料" width="80%">
      <el-form :model="receiveForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="退料单号">
              <el-input v-model="receiveForm.refundNo" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商">
              <el-input v-model="receiveForm.supplierName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收料仓库" required>
              <el-select
                v-model="receiveForm.warehouse"
                placeholder="请选择仓库"
              >
                <el-option
                  v-for="item in warehouseOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-table :data="receiveForm.items" border>
          <el-table-column prop="materialCode" label="物料编码" />
          <el-table-column prop="materialName" label="物料名称" />
          <el-table-column prop="refundQuantity" label="退料数量" />
          <el-table-column prop="receivedQuantity" label="已收数量" />
          <el-table-column prop="remainQuantity" label="待收数量" />
          <el-table-column label="本次收料数量" width="200">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.currentQuantity"
                :min="0"
                :max="scope.row.remainQuantity"
              />
            </template>
          </el-table-column>
          <el-table-column label="质检状态" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.qcStatus">
                <el-option label="合格" value="qualified" />
                <el-option label="待检" value="pending" />
                <el-option label="不合格" value="unqualified" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="备注" width="200">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>

        <div class="mt-20">
          <el-form-item label="备注">
            <el-input v-model="receiveForm.remark" type="textarea" />
          </el-form-item>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="receiveDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleReceiveSubmit"
            >确认收料</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

// ... 省略部分代码 ...

const handleReceiveSubmit = () => {
  if (!receiveForm.warehouse) {
    ElMessage.warning("请选择收料仓库");
    return;
  }

  // 验证收料数量
  const hasQuantity = receiveForm.items.some(
    (item) => item.currentQuantity > 0
  );
  if (!hasQuantity) {
    ElMessage.warning("请输入收料数量");
    return;
  }

  // 实现收料逻辑
  ElMessage.success("收料成功");
  receiveDialogVisible.value = false;
};
</script>

<style scoped>
.refund-receipt-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.mt-20 {
  margin-top: 20px;
}
</style>
